﻿package core.drawing
{
    import core.enum.*;
    import core.ui.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;

    final public class MoveTool extends Tool
    {
        var pt:Point;
        var po:Point;
        var pr:Point;

        public function MoveTool(param1:Tuyitu)
        {
            super(param1, ToolType.Move, true);
            param1.stage.addEventListener(KeyboardEvent.KEY_DOWN, this.KeyDown, false, 0, true);
            return;
        }// end function

        override public function Reset() : void
        {
            if (ed.canvas.layer.type == LayerType.Bitmap && ed.canvas.selection != null && ed.canvas.selection.IsSelected(ed.canvas.document.mouseX, ed.canvas.document.mouseY))
            {
                type.attitude.state = "cut";
            }
            else if (ed.canvas.document.anr != 0 || ed.canvas.document.transparent)
            {
                type.attitude.state = "normal";
            }
            else
            {
                type.attitude.state = "locked";
            }
            if (AppState.OnCanvas)
            {
                ed.cursor.Set(type);
            }
            return;
        }// end function

        override public function MouseMove(event:MouseEvent) : void
        {
            if (ed.canvas.selection != null)
            {
                if (ed.canvas.selection.IsSelected(ed.canvas.document.mouseX, ed.canvas.document.mouseY))
                {
                    if (type.attitude.state != "cut")
                    {
                        this.Reset();
                    }
                }
                else if (type.attitude.state == "cut")
                {
                    this.Reset();
                }
            }
            return;
        }// end function

        override public function MouseDown(event:MouseEvent) : void
        {
            if (type.attitude.state != "locked")
            {
                this.UpdateReferences();
                if (layer.type != LayerType.Bitmap || layer.hasBitmap)
                {
                    ed.cursor.freeze = true;
                    canvas.back.removeEventListener(MouseEvent.MOUSE_MOVE, this.MouseMove, false);
                    ed.addEventListener(MouseEvent.MOUSE_MOVE, this.MouseDrag, false, 1, true);
                    ed.addEventListener(MouseEvent.MOUSE_UP, this.MouseUp, false, 1, true);
                    this.pt = canvas.GetMousePosition();
                    this.pr = canvas.GetMousePosition();
                    switch(type.attitude.state)
                    {
                        case "normal":
                        {
                            history.Prepare(Action.LayerMove);
                            break;
                        }
                        case "cut":
                        {
                            history.Prepare(Action.MoveCut);
                            layer.SetBitmapCutEditMode(selection, Boolean(document.anr == 0 && !document.transparent), false);
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                }
            }
            return;
        }// end function

        override public function MouseDrag(event:MouseEvent) : void
        {
            this.po = canvas.GetMousePosition();
            switch(type.attitude.state)
            {
                case "normal":
                {
                    this.pt.x = this.po.x - this.pt.x;
                    this.pt.y = this.po.y - this.pt.y;
                    document.MoveLayer(this.pt);
                    break;
                }
                case "cut":
                {
                    layer.editLayer.x = layer.editLayer.x + (this.po.x - this.pt.x);
                    layer.editLayer.y = layer.editLayer.y + (this.po.y - this.pt.y);
                    this.pt.x = this.po.x - this.pt.x;
                    this.pt.y = this.po.y - this.pt.y;
                    selection.Move(this.pt);
                    canvas.coating.Clear();
                    canvas.coating.Redraw();
                    break;
                }
                default:
                {
                    break;
                }
            }
            this.pt = this.po;
            return;
        }// end function

        override public function MouseUp(event:MouseEvent) : void
        {
            var _loc_2:Canvas = null;
            var _loc_3:int = 0;
            var _loc_4:BitmapData = null;
            ed.cursor.freeze = false;
            ed.removeEventListener(MouseEvent.MOUSE_MOVE, this.MouseDrag, false);
            ed.removeEventListener(MouseEvent.MOUSE_UP, this.MouseUp, false);
            canvas.back.addEventListener(MouseEvent.MOUSE_MOVE, this.MouseMove, false, 0, true);
            switch(type.attitude.state)
            {
                case "normal":
                {
                    if (this.pr.x != this.pt.x && this.pr.y != this.pt.y)
                    {
                        history.Commit();
                    }
                    break;
                }
                case "cut":
                {
                    if (selection != null && (selection.ox != 0 || selection.oy != 0))
                    {
                        if (ed.canvas.masking.hitTestPoint(ed.stage.mouseX, ed.stage.mouseY))
                        {
                            selection.ResetOffset();
                            ed.navigator.SetSelectionSize(selection.width, selection.height);
                            history.Commit();
                        }
                        else
                        {
                            this.po = ed.canvas.GetMousePosition();
                            selection.ox = 0;
                            selection.oy = 0;
                            layer.editLayer.x = layer.editLayer.x + (this.pr.x - this.po.x);
                            layer.editLayer.y = layer.editLayer.y + (this.pr.y - this.po.y);
                            _loc_3 = ed.area.numChildren - 1;
                            while (_loc_3 >= 0)
                            {
                                
                                if ((ed.area.getChildAt(_loc_3) as Canvas).masking.hitTestPoint(ed.stage.mouseX, ed.stage.mouseY))
                                {
                                    _loc_2 = ed.area.getChildAt(_loc_3) as Canvas;
                                    if (_loc_2 != ed.canvas)
                                    {
                                        _loc_4 = (layer.editLayer as Bitmap).bitmapData;
                                        ed.ActivateCanvas(_loc_2);
                                        if (_loc_4 != null)
                                        {
                                            ed.cm.Paste(_loc_4);
                                        }
                                    }
                                    break;
                                }
                                _loc_3 = _loc_3 - 1;
                            }
                        }
                    }
                    layer.RemoveBitmapEditMode();
                    ed.canvas.coating.Clear();
                    ed.canvas.coating.Redraw();
                    break;
                }
                default:
                {
                    break;
                }
            }
            if (!AppState.OnCanvas)
            {
                ed.cursor.Set(ToolType.None);
            }
            ed.canvas.UpdateAfterEdit();
            return;
        }// end function

        public function KeyDown(event:KeyboardEvent) : void
        {
            var _loc_2:Point = null;
            var _loc_3:int = 0;
            if (event.keyCode >= 37 && event.keyCode <= 40 && ed.cm.CheckCanvas() && ed.cm.CheckNotLockedLayer())
            {
                _loc_2 = new Point();
                _loc_3 = event.shiftKey ? (10) : (1);
                switch(event.keyCode)
                {
                    case 37:
                    {
                        _loc_2.x = _loc_2.x - _loc_3;
                        break;
                    }
                    case 38:
                    {
                        _loc_2.y = _loc_2.y - _loc_3;
                        break;
                    }
                    case 39:
                    {
                        _loc_2.x = _loc_2.x + _loc_3;
                        break;
                    }
                    case 40:
                    {
                        _loc_2.y = _loc_2.y + _loc_3;
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
                ed.canvas.document.MoveLayer(_loc_2);
                ed.canvas.UpdateAfterEdit();
            }
            return;
        }// end function

        override public function CleanUp() : void
        {
            ed.stage.removeEventListener(KeyboardEvent.KEY_DOWN, this.KeyDown, false);
            type.attitude.state = "normal";
            return;
        }// end function

    }
}
